package com.yinhai.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.yinhai.adapter.CloudNoteSortAdapter;
import com.yinhai.cfg.AppConfig;
import com.yinhai.model.CloudNote;
import com.yinhai.service.CloudNoteServiceImpl;
import com.yinhai.util.GetCurrentTime;

public class CloudNoteSortActivity extends Activity {
	private String phone, sort_title, add_title;
	private ImageView iv_back, iv_add;
	private LinearLayout rl_main;
	EditText secondPwd, addEditText;
	int position;
	CloudNote editCloudNote;
	CloudNote selectCn;
	ListView popSortListView;
	BaseAdapter adapter = null;

	List<CloudNote> dataList;

	private CloudNoteServiceImpl cnServiceIpml;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cloudnotesort);
		cnServiceIpml = new CloudNoteServiceImpl(CloudNoteSortActivity.this);
		SharedPreferences preferences = getSharedPreferences(
				AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
		phone = preferences.getString("name", "");

		rl_main = (LinearLayout) findViewById(R.id.note_main);
		iv_back = (ImageView) findViewById(R.id.btn_back);
		iv_add = (ImageView) findViewById(R.id.btn_add);
		popSortListView = (ListView) findViewById(R.id.listview);
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		init();

		// 添加分类
		iv_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LayoutInflater factory = LayoutInflater
						.from(CloudNoteSortActivity.this);
				final View addEntryView = factory.inflate(
						R.layout.dialog_sortcloudnote, null);
				addEditText = (EditText) addEntryView
						.findViewById(R.id.sort_edit);

				AlertDialog dlg = new AlertDialog.Builder(
						CloudNoteSortActivity.this)
						.setIcon(0)
						.setTitle("添加分类")
						.setView(addEntryView)
						.setPositiveButton("保存",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										add_title = addEditText.getText()
												.toString().trim();
										if (add_title.equals("")) {
											Toast.makeText(
													CloudNoteSortActivity.this,
													"分类名称不能为空!",
													Toast.LENGTH_SHORT).show();
										} else {
											if (cnServiceIpml
													.isSameSortName(add_title)) {
												Toast.makeText(
														CloudNoteSortActivity.this,
														"分类名称已存在!",
														Toast.LENGTH_SHORT)
														.show();
											} else {
												CloudNote cn = new CloudNote();
												cn.setCreate_time(GetCurrentTime
														.getTime());
												cn.setUuid(GetCurrentTime
														.getUuid());
												cn.setPid("0");
												cn.setNote(add_title);
												cn.setPhone(phone);
												cnServiceIpml.insert(cn);
												dataList.add(cn);
												adapter.notifyDataSetChanged();
											}

										}

									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.dismiss();
									}
								}).create();
				dlg.show();
			}
		});

		// 编辑分类
		popSortListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						editCloudNote = dataList.get(arg2);
						LayoutInflater factory = LayoutInflater
								.from(CloudNoteSortActivity.this);
						View editEntryView = factory.inflate(
								R.layout.dialog_sortcloudnote, null);
						secondPwd = (EditText) editEntryView
								.findViewById(R.id.sort_edit);
						secondPwd.setText(dataList.get(arg2).getNote());

						if (dataList.get(arg2).getUuid().equals("1")) {
							Toast.makeText(CloudNoteSortActivity.this,
									"默认分类不能被编辑!", Toast.LENGTH_SHORT).show();
						} else {
							AlertDialog dlg = new AlertDialog.Builder(
									CloudNoteSortActivity.this)
									.setIcon(0)
									.setTitle("编辑记事本")
									.setView(editEntryView)
									.setPositiveButton(
											"保存",
											new DialogInterface.OnClickListener() {
												public void onClick(
														DialogInterface dialog,
														int whichButton) {
													sort_title = secondPwd
															.getText()
															.toString().trim();
													if (sort_title.equals("")) {
														Toast.makeText(
																CloudNoteSortActivity.this,
																"分类名称不能为空!",
																Toast.LENGTH_SHORT)
																.show();
													} else {
														if (cnServiceIpml
																.isSameSortName(sort_title)) {
															Toast.makeText(
																	CloudNoteSortActivity.this,
																	"分类名称已存在!",
																	Toast.LENGTH_SHORT)
																	.show();
														} else {
															editCloudNote
																	.setNote(sort_title);
															cnServiceIpml
																	.update(editCloudNote
																			.getUuid(),
																			editCloudNote
																					.getNote(),
																			GetCurrentTime
																					.getTime());
															adapter.notifyDataSetChanged();

															Intent imgroupBroadcast = new Intent(
																	"com.cloudnote.update");
															imgroupBroadcast
																	.putExtra(
																			"bc_uuid",
																			editCloudNote
																					.getUuid());
															imgroupBroadcast
																	.putExtra(
																			"is_del",
																			"0");
															CloudNoteSortActivity.this
																	.sendBroadcast(imgroupBroadcast);
														}

													}

												}
											})
									.setNegativeButton(
											"取消",
											new DialogInterface.OnClickListener() {
												public void onClick(
														DialogInterface dialog,
														int whichButton) {
													dialog.dismiss();
												}
											}).create();
							dlg.show();
						}

					}
				});

	}

	public OnClickListener DelItme = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			selectCn = (CloudNote) v.getTag();
			// TODO Auto-generated method stub
			if (selectCn.getUuid().equals("1")) {
				Toast.makeText(CloudNoteSortActivity.this, "默认分类不能删除",
						Toast.LENGTH_SHORT).show();
			} else {
				AlertDialog dlg = new AlertDialog.Builder(
						CloudNoteSortActivity.this)
						.setIcon(0)
						.setTitle("删除分类")
						.setMessage("删除该分类将删除该分类下所有记事消息")
						.setPositiveButton("删除",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										Intent imgroupBroadcast = new Intent(
												"com.cloudnote.update");
										imgroupBroadcast.putExtra("bc_uuid",
												selectCn.getUuid());
										imgroupBroadcast
												.putExtra("is_del", "1");
										CloudNoteSortActivity.this
												.sendBroadcast(imgroupBroadcast);
										cnServiceIpml = new CloudNoteServiceImpl(
												CloudNoteSortActivity.this);
										cnServiceIpml.delete(selectCn);
										dataList.remove(selectCn);
										adapter.notifyDataSetChanged();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.dismiss();
									}
								}).create();
				dlg.show();
			}
		}
	};

	void init() {
		new SortAsyncTask().execute();
	}

	private class SortAsyncTask extends AsyncTask<Void, Void, Integer> {
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			popSortListView.setAdapter(adapter);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			// TODO Auto-generated method stub
			dataList = cnServiceIpml.getSortList("0");
			adapter = new CloudNoteSortAdapter(CloudNoteSortActivity.this,
					dataList);
			return 0;
		}

	}

}
